<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="CommonUtil/CommonUtil.js"></script>
    <script type="text/javascript" charset="utf-8">
        /**
         * 场景模拟
         * -->公司
         *       -->北京分公司
         *                   -->开发部门
         *                              -->张一
         *                              -->张二
         *                              -->张三
         *                   -->销售部门
         *                              -->张四
         *                              -->张五
         *                              -->张六
         *          -->长沙分公司
         *                   -->开发部门
         *                              -->张七
         *                              -->张八
         *                              -->张九
         *                   -->销售部门
         *                              -->张十
         *                              -->张十一
         *                              -->张十二
         *
         */
        var CompositeInterface =  new BH.Interface('CompositeInterface',['addChild','getChild']);
        var LeafInterface = new BH.Interface('LeafInterface',['hardWorking,sleeping']);
        var Composite = function (name) {
            this.name = name;
            this.type = 'Composite';
            this.children= [];
        }
        Composite.prototype = {
            constructor:Composite,
            addChild:function (child) {
                this.children.push(child);
                return this;
            },
            getChild:function (name) {
                //根据name让指定name下所有的类型为leaf的对象去执行操作
                var elements = [];
                //判断是否是leaf类型
                var pushLeaf = function (item) {
                    if(item.type==='Composite'){
                        item.children.each(arguments.callee);
                    }else if(item.type==='leaf'){
                        elements.push(item);
                    }
                };
                if(name &&this.name!==name){
                    this.children.each(function (item) {
                        if(item.name===name && item.type=='Composite'){
                            item.children.each(pushLeaf);
                        }
                        //如果name是3级，4级，5级
                        if(item.name!==name && item.type ==='Composite') {
                            item.children.each(arguments.callee);
                        }
                        //如果传递的是叶子节点
                        if(item.name===name && item.type==='leaf'){
                            elements.push(item);
                        }
                    })
                }else{     //整个公司所有类型为leaf的对象去执行操作
                    this.children.each(pushLeaf);
                }
                return elements;
            },
            hardWorking:function (name) {
               var leafObjects = this.getChild(name);
               for(var i = 0;i<leafObjects.length;i++){
                   leafObjects[i].hardWorking();
               }
            },
            sleeping:function (name) {
                var leafObjects = this.getChild(name);
                for(var i = 0;i<leafObjects.length;i++){
                    leafObjects[i].sleeping();
                }

            }
        }
        var Leaf = function (name) {
            this.name = name;
            this.type = 'leaf';
        }
        Leaf.prototype = {
            constructor:Leaf,
            addChild:function (child) {
                throw new Error('this method is disabled...');
            },
            getChild:function (name) {
                if(this.name===name){
                    return this;
                }
                return null;
            },
            hardWorking:function (name) {
                document.write(this.name+'...努力工作...');
            },
            sleeping:function (name) {
                document.write(this.name+'...努力睡觉...');
            }
        }
        //测试数据
        var p1 = new Leaf('zhang1');
        var p2 = new Leaf('zhang2');
        var p3 = new Leaf('zhang3');
        var p4 = new Leaf('zhang4');
        var p5 = new Leaf('zhang5');
        var p6 = new Leaf('zhang6');
        var p7 = new Leaf('zhang7');
        var p8 = new Leaf('zhang8');
        var p9 = new Leaf('zhang9');
        var p10 = new Leaf('zhang10');
        var p11 = new Leaf('zhang11');
        var p12 = new Leaf('zhang12');
        var dept1 = new Composite('北京开发部门');
        dept1.addChild(p1).addChild(p2).addChild(p3);
        var dept2 = new Composite('北京销售部门');
        dept2.addChild(p4).addChild(p5).addChild(p6);
        var dept3 = new Composite('长沙开发部门');
        dept3.addChild(p7).addChild(p8).addChild(p9);
        var dept4 = new Composite('长沙销售部门');
        dept4.addChild(p10).addChild(p11).addChild(p12);
        var org1 = new Composite('北京分公司');
        org1.addChild(dept1).addChild(dept2);
        var org2 = new Composite('长沙分公司');
        org2.addChild(dept3).addChild(dept4);
        var org = new Composite('中软国际');
        org.addChild(org1).addChild(org2);
        //整个公司
        org.hardWorking();
        //name为总公司的直接子节点
        org.hardWorking('长沙分公司');
        //name为总公司的间接子节点类型不为Leaf
        org.hardWorking('长沙开发部门');
        //name为Leaf对象
        org.hardWorking('zhang10');
    </script>
</head>
<body>
</body>
</html>